using System;
using System.Text;
using System.IO;
using System.Xml;
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;



namespace xmloutput
{
    class xmloutput
    {
        static void Main(string[] args)
        {

            string inputfile = "";
            string lcsqltext = "";

            if (args.Length == 0)
            {
                Console.WriteLine("xmloutput scriptfile");
                return;
            }

            inputfile = args[0];

            //Reading from file
            try
            {
                StreamReader re = File.OpenText(inputfile); //new StreamReader(inputfile,new UnicodeEncoding());//
                // Console.WriteLine("The encoding used was {0}.", re.CurrentEncoding);

                string input = null;
                while ((input = re.ReadLine()) != null)
                {
                    lcsqltext = lcsqltext + Environment.NewLine + input;
                    // Console.WriteLine(input);
                }
            }
            catch (Exception e)
            {
                // Let the user know what went wrong.
                Console.WriteLine("File could not be read:");
                Console.WriteLine(e.Message);
                return;
            }

            // create a sql parser instance supports ms sql server sql dialect
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVOracle); //DbVOracle

            sqlparser.SqlText.Text = lcsqltext;// "select dummytable.dummyfield from dummytable";
            int iRet = sqlparser.Parse();
            if (iRet == 0)
            {
                //Console.WriteLine(basicnodexml.TlzFieldXml(sqlparser.SqlStatements[0].Fields[0]));

                // Console.WriteLine(sqlparser.SqlStatements[0].Fields[0].FieldAttr.AsText);
                //if (sqlparser.SqlStatements[0].Fields[0].FieldAttr != null)
                //{
                //  TLz_Attr a = sqlparser.SqlStatements[0].Fields[0].FieldAttr;
                //  Console.WriteLine(basicnodexml.TLz_AttrXml(a));
                //}

                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Indent = true;
                settings.IndentChars = ("    ");

                StringBuilder XmlText = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(XmlText, settings))
                {
                    // Write XML data.
                    writer.WriteRaw("<?xml-stylesheet type=\"text/xsl\" href=\"tree-view.xsl\"?>");
                    writer.WriteStartElement("sqlscript");
                    writer.WriteAttributeString("dbvendor", sqlparser.DbVendor.ToString());
                    foreach (TCustomSqlStatement sql in sqlparser.SqlStatements)
                    {
                        writer.WriteRaw(commonstmt.TSqlStatementXml(sql));
                    }
                    writer.WriteEndElement();
                    writer.Flush();
                }

                //Console.WriteLine(XmlText.ToString());

                using (StreamWriter streamWriter =
                            new StreamWriter(inputfile+".xml"))
                {
                    streamWriter.Write(XmlText.ToString());
                    streamWriter.Flush();
                }


            }
            else
            {
                Console.WriteLine("Syntax error found in input sql:");
                Console.WriteLine(sqlparser.ErrorMessages);
            }

        }
    }
}
